package work.shuangzhizhen;

import java.util.Arrays;

public class Problem283 {
//    public void moveZeroes(int[] nums) {
//        int count = 0;
//        for (int i = 0; i < nums.length; i++) {
//            if (i == nums.length - count) {
//                break;
//            }
//            if (nums[i] == 0) {
//                count++;
//                int j = i;
//                for (; j < nums.length - count; j++) {
//                    nums[j] = nums[j + 1];
//                }
//                i--;
//                nums[j] = 0;
//            }
//        }
//    }


    public void moveZeroes(int[] nums) {
        int j = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] != 0) {
                nums[j] = nums[i];
                j++;
            }
            for (; j < nums.length; j++) {
                nums[j] = 0;
            }
        }
    }


    //双指针
    public void moveZeroes1(int[] nums) {
        if (nums == null) {
            return;
        }
        //两个指针i和j
        int j = 0;
        for (int i = 0; i < nums.length; i++) {
            //当前元素!=0，就把其交换到左边，等于0的交换到右边
            if (nums[i] != 0) {
                int tmp = nums[i];
                nums[i] = nums[j];
                nums[j++] = tmp;

            }

        }
        System.out.println(Arrays.toString(nums));
    }

    public static void main(String[] args) {
        int[] a = {2, 1, 0, 4, 9, 0, 0, 10};
        Problem283 peo = new Problem283();
        peo.moveZeroes1(a);

    }

    public void moveZeroes2(int[] nums) {
        int fast = nums.length - 1, slow = 0;
        while (fast > slow) {
            if (nums[slow] == 0) {

            }
        }
    }

}
